# This file is part of GPropertyGrid project. 
# The COPYRIGHT file at the top level of this repository 
# contains the full copyright notices and license terms.

from gi.repository import Gtk, GObject
from . propertygridproperty import PropertyGridProperty

class PropertyGrid(Gtk.Box, GObject.GObject):
    __gsignals__ = {
        'changed': (GObject.SIGNAL_RUN_FIRST, None,
                      (PropertyGridProperty,))
    }

    def __init__(self, title):
        Gtk.Box.__init__(self, 
                    orientation=Gtk.Orientation.VERTICAL, 
                    spacing=1)
        GObject.GObject.__init__(self)
        
        self._groups=[]
        self._properties=[]
        self._property_names={}
        
        self._next_id=-1
        
        label = Gtk.Label(xalign=0)
        label.set_name("property_grid_header")
        label.set_text(title)
        
        self.pack_start(label, False, False, 0)
        
        self._sw = Gtk.ScrolledWindow()
        self.pack_start(self._sw, False, False, 0)
        
        self._row = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, 
                                            spacing=1)
        self._sw.add(self._row)
        self.connect("draw", self._on_draw)

    @property
    def properties(self):
        return self._properties
        
    def add_group(self, group):
        self._groups.append(group)
        group._grid=self
        self._row.pack_start(group, False, False, 0)        
        
    def get_property_by_id(self, id):
        if not id in self._property_names:
            return
        return self._property_names[id]        
        
    def _on_draw(self, wg, data):
        self._sw.set_size_request(self.get_allocated_width() - 50,
            self.get_allocated_height() - 50)      
        
    def _add_property(self, group, property_):
        property_._group = group
        if property_.id == None:
            property_.id="property_{0}".format(self._next_id)
            self._next_id=self._next_id+1
        if property_.id in self._property_names:
            property_.value_widget.set_sensitive(False)
            raise ValueError("Properpy {0} already exists in PropertyGrid".format(property_.id))
        self._properties.append(property_)
        self._property_names[property_.id]=property_
        group._properties.append(property_)

    def _on_enter_widget(self, id):
        for p in self._properties:
            if p.id == id:
                continue
            if p._has_focus:
                p.on_leave()
                p.show_hide_value_widget()
                
    def _property_changed(self, property_):
        self.emit("changed", property_)
        

class PropertyGridGroup(Gtk.Expander):
    def __init__(self, name):
        super(PropertyGridGroup, self).__init__()
        self._grid=None
        self._properties=[]
        self.set_name("group_header")
        self.name=self._properties=[]
        
        self._row = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, 
                                            spacing=0)
        
        label = Gtk.Label(xalign=0)
        label.set_name("group_header_label")
        label.set_text(name)
        self.set_label_widget(label)
        self.set_label_fill(True)
        
        self.add(self._row)

    @property
    def grid(self):
        return self._grid
        
    @property
    def properties(self):
        return self._properties
        
    def add_property(self, property_):
        self._grid._add_property(self, property_)
        self._row.pack_start(property_, False, False, 0)
        
